package com.huilian.iotbox.admin.service.impl;

import com.huilian.iotbox.admin.service.StatsLedgerMerchantOrderExchangeService;
import com.huilian.iotbox.admin.service.StatsLedgerMerchantService;
import com.huilian.iotbox.data.dto.StatsLedgerMerchantDto;
import com.huilian.iotbox.data.enums.StatisticsTypeEnum;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.vo.StatsLedgerMerchantVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.List;

@Slf4j
@Service
public class StatsLedgerMerchantOrderExchangeServiceImpl implements StatsLedgerMerchantOrderExchangeService {
    @Autowired
    private StatsLedgerMerchantService statsLedgerMerchantService;
    @Autowired
    private ErrorLogCommonService errorLogCommonService;
    /**
     * 统计分账：单日
     * */
    @Override
    public Exception StatsLedgerMerchantOrderExchangeJob() {
        //获取昨天 1970-01-01 00：00：00
        String startTime = DateTimeUtils.getYesterdayStartTime();
        //获取昨天 1970-01-01 23：59：59
        String endTime = DateTimeUtils.getYesterdayEndTime();

        // 昨天，1970-01-01
        String yesterday = DateTimeUtils.getYesterday();

        Exception temp = null;
        try {
            List<StatsLedgerMerchantVo> statsLedgerMerchantVoList = statsLedgerMerchantService.finLedgerStatistics(startTime,endTime);
            if(statsLedgerMerchantVoList.size()>0){
                for (StatsLedgerMerchantVo smv:statsLedgerMerchantVoList){
                    StatsLedgerMerchantDto statsLedgerMerchantDto = new StatsLedgerMerchantDto();
                    statsLedgerMerchantDto.setUserId(smv.getUserId());
                    statsLedgerMerchantDto.setMerchantStoreId(smv.getMerchantStoreId());
                    statsLedgerMerchantDto.setType(StatisticsTypeEnum.DAY.getCode());
                    statsLedgerMerchantDto.setMerchantId(smv.getMerchantId());
                    statsLedgerMerchantDto.setDateStr(yesterday);
                    statsLedgerMerchantDto.setMoney(smv.getLedgerMoney());
                    statsLedgerMerchantDto.setCostAmount(smv.getCostAmount());
                    statsLedgerMerchantDto.setLedgerType(smv.getLedgerType());
                    statsLedgerMerchantDto.setOrderType(smv.getOrderType());
                    statsLedgerMerchantService.addStatsledgerMerchant(statsLedgerMerchantDto);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            temp = e;
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            StatsLedgerMerchantDto statsLedgerMerchantDto = new StatsLedgerMerchantDto();
            statsLedgerMerchantDto.setStartTime(startTime);
            statsLedgerMerchantDto.setEndTime(endTime);
            errorLogCommonService.writerInfoException("分账订单日统计", statsLedgerMerchantDto, e);
        }
        return temp;
    }

    /**
     * 统计分账：单月
     * */
    @Override
    public Exception StatsMonthLedgerMerchantOrderExchangeJob() {
        //获取上个月开始时间 1970-01-01 00：00：00
        String startTime = DateTimeUtils.getPreviousMonthFirstDayTime();
        //获取上个月结束时间 1970-01-31 23：59：59
        String endTime = DateTimeUtils.getPreviousMonthLastDayTime();
        String previousMonth = DateTimeUtils.getPreviousMonth();
        Exception temp = null;
        try {
            List<StatsLedgerMerchantVo> statsLedgerMerchantVoList = statsLedgerMerchantService.finLedgerStatistics(startTime,endTime);
            if(statsLedgerMerchantVoList.size()>0){
                for (StatsLedgerMerchantVo smv:statsLedgerMerchantVoList){
                    StatsLedgerMerchantDto statsLedgerMerchantDto = new StatsLedgerMerchantDto();
                    statsLedgerMerchantDto.setUserId(smv.getUserId());
                    statsLedgerMerchantDto.setMerchantStoreId(smv.getMerchantStoreId());
                    statsLedgerMerchantDto.setType(StatisticsTypeEnum.MONTH.getCode());
                    statsLedgerMerchantDto.setMerchantId(smv.getMerchantId());
                    statsLedgerMerchantDto.setDateStr(previousMonth);
                    statsLedgerMerchantDto.setMoney(smv.getLedgerMoney());
                    statsLedgerMerchantDto.setCostAmount(smv.getCostAmount());
                    statsLedgerMerchantDto.setLedgerType(smv.getLedgerType());
                    statsLedgerMerchantDto.setOrderType(smv.getOrderType());
                    statsLedgerMerchantService.addStatsledgerMerchant(statsLedgerMerchantDto);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            temp = e;
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            StatsLedgerMerchantDto statsLedgerMerchantDto = new StatsLedgerMerchantDto();
            statsLedgerMerchantDto.setStartTime(startTime);
            statsLedgerMerchantDto.setEndTime(endTime);
            errorLogCommonService.writerInfoException("分账订单月统计", statsLedgerMerchantDto, e);
        }
        return temp;
    }
}
